import * as userService from '../../services/user';

export default {
  namespace: 'user',
  state: {
    deptTree: [],
    userList: [],
    roleList: [],
    current : 1,
    currentSize : 10,
    total : 0, 
    deptId : 1,  
    currentItem: {},
    modalVisible : false,
    modalType : 'create',
    newKey : '',
  },
  reducers: {
    qryDeptsSuc(state, { payload: deptTree }) {
      return { ...state, deptTree };
    },
    qryUserByParamsSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    qryRolesSuc(state, { payload: roleList }) {
      return { ...state, roleList };
    },
    showModal (state, { payload }) {
      return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false, newKey : new Date().getTime() + '' }
    },
  },
  effects: {
    *qryDepts({ payload },{ call, put }) {    
      const { body }  = yield call(userService.qryDepts);
      yield put({ type: 'qryDeptsSuc', payload: body.data });
    },
    *qryUserByParams({ payload },{ call, put }) {
      const { body } = yield call(userService.qryUserByParams,payload);
      yield put({ type: 'qryUserByParamsSuc', payload: { userList:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords, deptId:payload.deptId }});
    },
    *qryRoles({payload},{ call, put }) {
      const { body }  = yield call(userService.qryRoles,payload);
      yield put({ type: 'qryRolesSuc', payload: body.data });
    },
    *create({ payload },{ call, put }){
      const { body } = yield call(userService.create,payload);
      yield put({ type: 'hideModal' });
      yield put({ type: 'reload' });
    },
    *qryUserById({ payload },{ call, put }){
      const { body } = yield call(userService.qryUserById,payload);
      yield put({
          type: 'showModal',
          payload: {
            currentItem: body.data,
            modalType: 'edit',
            title: '编辑用户',
          },
      });
    },
    *edit({ payload },{ call, put }){
      const { body } = yield call(userService.edit,payload);
      yield put({ type: 'hideModal' });
      yield put({ type: 'reload' });
    },
    *remove({ payload },{ call, put }){
      const { body } = yield call(userService.remove,payload.id);
      yield put({ type: 'qryUserByParams', payload:payload.search });
      yield put({ type: 'qryDepts' });
    },
    *reload(action, { put }) {
      yield put({ type: 'qryUserByParams', payload:{}});
      yield put({ type: 'qryDepts' });
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/sys/user') {
          dispatch({ type: 'qryDepts' });
          dispatch({ type: 'qryUserByParams', payload:{}});
          dispatch({ type: 'qryRoles', payload:{ 'name':'' }});
        }
      });
    },
  },
};
